<template>
  <div class="app-container">
    <el-form inline :model="listQuery" size="small" label-width="100px">
      <el-form-item label="输入搜索：">
        <el-input v-model="listQuery.keyword" class="input-width" placeholder="帐号/姓名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSearchList" size="small"> 搜索 </el-button>
        <el-button @click="handleResetSearch" size="small"> 重置 </el-button>
        <el-button type="primary" size="small" class="btn-add" @click="handleUpsert"> 添加 </el-button>
      </el-form-item>
    </el-form>
    <div class="table-container">
      <el-table ref="adminTable" height="100%" :data="list" style="width: 100%" v-loading="listLoading" border>
        <el-table-column label="编号" min-width="100" align="center">
          <template #default="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column label="帐号" min-width="160" align="center">
          <template #default="scope">{{ scope.row.username }}</template>
        </el-table-column>
        <el-table-column label="姓名" min-width="160" align="center">
          <template #default="scope">{{ scope.row.nickName }}</template>
        </el-table-column>
        <el-table-column label="邮箱" min-width="160" align="center">
          <template #default="scope">{{ scope.row.email }}</template>
        </el-table-column>
        <el-table-column label="添加时间" min-width="180" align="center">
          <template #default="scope">{{ formatDateTime(scope.row.createTime) }}</template>
        </el-table-column>
        <el-table-column label="最后登录" min-width="180" align="center">
          <template #default="scope">{{ formatDateTime(scope.row.loginTime) }}</template>
        </el-table-column>
        <el-table-column label="是否启用" min-width="100" align="center">
          <template #default="scope">
            <el-switch
              v-model="scope.row.status"
              size="small"
              :active-value="1"
              :inactive-value="0"
              @change="handleStatusChange(scope.row)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180" align="center">
          <template #default="scope">
            <el-button type="primary" size="small" link @click="handleSelectRole(scope.row)"> 分配角色 </el-button>
            <el-button type="primary" size="small" link @click="handleUpsert(scope.row)"> 编辑 </el-button>
            <el-button type="danger" size="small" link @click="handleDelete(scope.row)"> 删除 </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        v-model:current-page="listQuery.pageNum"
        background
        layout="total, sizes,prev, pager, next,jumper"
        :page-size="listQuery.pageSize"
        :page-sizes="[10, 15, 20]"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <Upsert ref="upsertRef" @refreshDataList="getList" />
    <AllocRole ref="allocRoleRef" />
  </div>
</template>
<script setup lang="ts">
import { fetchList, updateStatus, deleteAdmin } from '@/api/user'
import { ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { formatDateTime } from '@/utils/format'
import Upsert from './components/upsert.vue'
import AllocRole from './components/allocRole.vue'
import type { User } from '@/models/user'

defineOptions({ name: 'AdminList' })

interface SearchForm {
  pageNum: number
  pageSize: number
  keyword?: string
}

const listQuery = ref<SearchForm>({ pageNum: 1, pageSize: 10 })
const list = ref<User[]>([])
const total = ref(0)
const listLoading = ref(false)

const upsertRef = ref()
const allocRoleRef = ref()

function handleResetSearch() {
  listQuery.value = { pageNum: 1, pageSize: 10 }
}

function handleSearchList() {
  listQuery.value.pageNum = 1
  getList()
}

function handleSizeChange(val: number) {
  listQuery.value.pageNum = 1
  listQuery.value.pageSize = val
  getList()
}

function handleCurrentChange(val: number) {
  listQuery.value.pageNum = val
  getList()
}

function handleUpsert(row?: User) {
  upsertRef.value.show(row?.id)
}

async function handleStatusChange(row: User) {
  try {
    await ElMessageBox.confirm('是否要修改该状态?', '提示', { type: 'warning' })
    await updateStatus(row.id as number, { status: row.status as number })
    ElMessage.success('修改成功!')
    getList()
  } catch {
    row.status = row.status === 1 ? 0 : 1
  }
}

async function handleDelete(row: User) {
  await ElMessageBox.confirm('是否要删除该用户?', '提示', { type: 'warning' })
  await deleteAdmin(row.id as number)
  ElMessage.success('删除成功！')
  getList()
}

function handleSelectRole(row: User) {
  allocRoleRef.value.show(row.id as number)
}

async function getList() {
  listLoading.value = true
  try {
    const response = await fetchList(listQuery.value)
    list.value = response.data.data.list
    total.value = response.data.data.total
  } finally {
    listLoading.value = false
  }
}

getList()
</script>
<style></style>
